package com.sgtech.demo.filter;


import com.sgtech.demo.model.SysUser;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.util.ObjectUtils;

import javax.servlet.*;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;

/**
 * @Description: 记住我过滤器
 * @Author sgtech
 * @Date 2021/7/14 1:17
 **/
@Slf4j
@Component
public class RememberFilter implements Filter {
    private String[] noFilterArray=new String[]{"/login","/dologin"};

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain filterChain) throws IOException, ServletException {
        List<String> noFilterList= Arrays.asList(noFilterArray);
        HttpServletRequest req = (HttpServletRequest) request;
        HttpServletResponse res = (HttpServletResponse) response;
        String requestURI = req.getRequestURI();
        requestURI = requestURI.substring(requestURI.lastIndexOf("/"));
        // 判断是否为不需要过滤请求
        if (noFilterList.contains(requestURI)) {
            filterChain.doFilter(request,response);
        }else{
            HttpSession session = req.getSession();
            if (null==session||session.getAttribute("user")==null) {
                Cookie[] cookies = req.getCookies();
                if (ObjectUtils.isEmpty(cookies)) {
                    ((HttpServletResponse) response).sendRedirect(req.getContextPath()+"/login");
                    return;
                } else {
                    SysUser user = null;
                    for(Cookie cookie:cookies){
                        String name = cookie.getName();
                        if (name.equals("boot_rememberMe")) {
                            String value = cookie.getValue();
                            // 演示采用硬编码，实际需要和数据库中token比较
                            if (!value.equals("YWRtaW4=")) {
                                Cookie cookie1=new Cookie("boot_rememberMe",null);
                                cookie1.setMaxAge(0);
                                res.addCookie(cookie1);
                            }else{
                                user = new SysUser();
                                user.setUsername("admin");
                                user.setPassword("admin");
                                user.setRememberMe(1);
                                session.setAttribute("user", user);
                                log.info("从cookie中获取user放入session");
                            }
                        }
                    }
                    if(user==null){
                        ((HttpServletResponse) response).sendRedirect(req.getContextPath()+"/login");
                        return;
                    }
                }
            }
            filterChain.doFilter(request,response);
        }

    }
}
